#pragma once

#include<string>

/**
Path utilities class
*/
class pgPath
{
public:

	/**
	Get the file extension of the given path or file.
	@param szFilePath Gets the extension of this string
	@returns New string containing the extension without dot
	*/
	static std::string GetExtension(const char* szFilePath)
	{
		std::string	buffer(szFilePath);
		size_t pos = buffer.find_last_of('.');

		if (pos == -1)
		{
			PG_ERROR(ER_INVALID_PATH);
		}
		return (buffer.substr(pos + 1, buffer.length() - pos + 1)); 
	}

	/**
	Changes the extension of the given filepath.
	@param szPath Path with extension to be changed
	@param szExtension New extension the path should get
	@returns New std::string containing the path with new extension
	*/
	static std::string ChangeExtension(const char* szPath, const char* szExtension)
	{
		std::string	buffer(szPath);
		size_t pos = buffer.find_last_of('.');
		return buffer.replace(pos + 1, buffer.length() - pos + 1, szExtension);
	}

	/**
	Extracts the filename from the given path.
	@param szPath Path containing the filename.
	@returns New std::string containing only the filename
	*/
	static std::string GetFileName(const char* szPath)
	{
		std::string	buffer(szPath);
		size_t pos0 = buffer.find_last_of("\\");
		size_t pos = buffer.find_last_of('.');
		return buffer.substr(pos0 + 1, pos - pos0 + 1 - 2);
	}

	/**
	Reads the specified directory and returns a std::string array with all filenames.
	@param lpPath The path to the directory to be searched followed by \\* (e.g. ".\\Data\\*")
	@param count Gets filled with the number of files found
	@returns std::string array with all found filenames, no paths!
	*/
	static std::string *GetFilesInDirectory(LPCWSTR lpcPath, int &count)
	{
		WIN32_FIND_DATA findData;

		
		HANDLE found = FindFirstFile(lpcPath, &findData);

		
		

		if (found == INVALID_HANDLE_VALUE)
		{
			PG_LOG_WARNING_EX(TOPIC_PGGAME_APP, "Nothing found when searching for files in directory: %s!", lpcPath);
			FindClose(found);
			return NULL;
		}

		do
		{
			if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
			{
				++count;
			}
		}while (FindNextFile(found, &findData));
		std::string *pStrings = NULL;

		if (count > 0)
		{
			pStrings = new NEW_MEM_LEAK std::string[count];

			found = FindFirstFile(lpcPath, &findData);
			int idx = 0;
			do
			{
				if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
				{
					pStrings[idx] = "";
					
					size_t origsize = wcslen(findData.cFileName) + 1;
					const size_t newsize = MAX_BUFFER;
					size_t convertedChars = 0;
					char nstring[newsize];
					wcstombs_s(&convertedChars, nstring, origsize, findData.cFileName, _TRUNCATE);

				
					pStrings[idx] = nstring;
					++idx;
				}
			}while (FindNextFile(found, &findData));
		}
		else
		{
			PG_LOG_WARNING_EX(TOPIC_PGGAME_APP, "No files found when searching for files in directory: %s!", lpcPath);
		}

		FindClose(found);

		return pStrings;

	}
};